package phapp.action.barapp;

import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import org.springframework.web.bind.annotation.RequestMapping;
import net.sf.json.*;
import javacommon.util.*;
import javacommon.jquery.*;
import static javacommon.util.Struts2JsonHelper.*;
import com.opensymphony.xwork2.*;
import cn.org.rapid_framework.page.*;

import phapp.model.barapp.Wifis;

import javacommon.base.*;

import phapp.model.barapp.*;
import phapp.dao.barapp.*;
import phapp.service.barapp.impl.*;
import phapp.service.barapp.*;

/**
 * <p> 标题: 酒吧wifi设备管理
 * <p>Title: tc_wifis_info, 
 * <p>Description: 接收前面界面请求,通过json数据格式交互 Action
 * <p>Description: 页面通过Ajax调用Action层,返回JSONO数据
 * <p>Copyright: Copyright (c) 2014-10
 * <p>Company:
 * @see phapp.model.barapp.Wifis
 * @see phapp.service.barapp.WifisManager 
 * @see phapp.service.barapp.impl.WifisManagerImpl
 * @see phapp.dao.barapp.WifisDao
 * @author you name
 * @version 1.0
*/


public class WifisAction extends BaseStruts2Action {

   private WifisManager wifisManager;

   /** 通过spring自动注入 */
   public void setWifisManager(WifisManager manager) {
      this.wifisManager = manager;
   }

   /**
    * <p> 标题:jqGrid使用列表,分页查询
    * @throws IOException 
    */
   public void list() throws IOException{
      PageRequest<Map> pr = JqueryPageHelper.createPageRequestForJquery(getRequest());
      Page page = wifisManager.findByPageRequest(pr);

      List<Wifis> Wifislist = (List) page.getResult();
      ListRange<Wifis> resultList = new ListRange<Wifis>();
      resultList.setDataRows(Wifislist);
        resultList.setRecords(page.getTotalCount());
        resultList.setRows(page.getPageSize());
      if(page.getTotalCount()%page.getPageSize()>0){
         resultList.setTotal(page.getTotalCount() / page.getPageSize()+1);
      }else{
         resultList.setTotal(page.getTotalCount() / page.getPageSize());
      }
      resultList.setPage(pr.getPageNumber());
      outJson(resultList);
   }

   /**
    * <p> 标题:Wifis添加 
    * @throws IOException
    */
   public void save() throws IOException{
      Map<String, Object> result = new HashMap<String, Object>();
      try{
         HttpServletRequest request = getRequest();
         String wifisJson = request.getParameter("wifisJson");
         if (null != wifisJson&&!"".equals(wifisJson)) {
            JSONObject jsonobject =JSONObject.fromObject( wifisJson );
            Wifis wifisobj = (Wifis)JSONObject.toBean(jsonobject, Wifis.class);
            wifisManager.save(wifisobj);
            result.put("success", true);
            result.put("msg", "添加成功!");
         }else{
            result.put("success", false);
            result.put("msg", "添加失败 数据错误!");
         }
      }catch (Exception e){
         result.put("success", false);
         result.put("msg", "添加失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   /**
    * <p> 标题:Wifis修改
    * @throws IOException 
    */
   public void update() throws IOException{
      Map<String, Object> result = new HashMap<String, Object>();
         try{
            HttpServletRequest request = getRequest();
            String wifisJson = request.getParameter("wifisJson");
            if (null != wifisJson&&!"".equals(wifisJson)) {
               JSONObject jsonobject =JSONObject.fromObject( wifisJson );
               Wifis wifisobj = (Wifis)JSONObject.toBean(jsonobject, Wifis.class);
               /**设置主键值*/
               String jdbcSqlTypeName="";
               wifisobj.setId(Integer.parseInt(jsonobject.get("id").toString()));
                
               wifisManager.update(wifisobj);  
               result.put("success", true);
               result.put("msg", "修改成功!");
            }else{
               result.put("success", false);
               result.put("msg", "修改失败 数据错误!");
            }
         }catch (Exception e){
            result.put("success", false);
            result.put("msg", "修改失败!");
            e.printStackTrace();
         }
      outJson(result);
   }

   /**
    * <p> 标题:Wifis删除
    * @throws IOException 
    */
   public void delete() throws IOException{
      Map<String, Object> result = new HashMap<String, Object>();
      try{
         HttpServletRequest request = getRequest();
         String ids = request.getParameter("ids");
         if (null != ids&&!"".equals(ids)){
            Wifis wifis =new Wifis();
            wifis.setIdList(StringTool.stringToArray(ids));
            wifisManager.removeByIdArry(wifis);
            result.put("success", true);
            result.put("msg", "删除成功");
         }else{
            result.put("success", false);
            result.put("msg", "删除失败 数据错误!");
         }
      }catch (Exception e){
         result.put("success", false);
         result.put("msg", "删除失败!");
         e.printStackTrace();
      }
      outJson(result);
   }
   /**
    * <p>标题:操作数据 新增和修改 
    * @throws IOException
    */
   public void newsave() throws IOException{
      Map<String, Object> result = new HashMap<String, Object>();
      try{
         HttpServletRequest request = getRequest();
         String wifisJson = request.getParameter("wifisJson");
         if (null != wifisJson&&!"".equals(wifisJson)) {
            JSONObject jsonobject =JSONObject.fromObject( wifisJson );
            Wifis wifisobj = (Wifis)JSONObject.toBean(jsonobject, Wifis.class);
            //有ID-修改,无ID新增
            if(null!=jsonobject.get("id").toString()&&jsonobject.get("id").toString().length()>0){
            	wifisManager.update(wifisobj);
            }else{
                wifisManager.save(wifisobj);
            }
            result.put("success", true);
            result.put("msg", "操作成功!");
         }else{
            result.put("success", false);
            result.put("msg", "操作失败 数据错误!");
         }
      }catch (Exception e){
         result.put("success", false);
         result.put("msg", "操作失败!");
         e.printStackTrace();
      }
      outJson(result);
   }
	/**
	 *  <p>标题：下拉框所用 列表 
	 * @throws IOException
	 */
	public void getComboBox() throws IOException { 
		Map<String, Object> result = new HashMap<String, Object>();
		List list = new ArrayList();
		try {
			String str = "";
			Wifis wifisobj = new Wifis();
			list = wifisManager.findAll();
	        result.put("success", false);
	        result.put("list", list);
		} catch (Exception e) { 
			e.printStackTrace();
		}
		outJson(list);
	}
	 
    /**
     * <p>标题: 查询列表数据 ByMap
     * <p>描述:  
     * @throws Exception 
     */
    public void  findListByMap() throws Exception{ 
    	Map map = getParamMap();
		List<Wifis> dataList = wifisManager.findList(map);
		Map<String,Object> result = new HashMap<String,Object>();
		result.put(SUCCESS, true);
		result.put("list", dataList);
		outJson( result);
    }
    /**
     * <p>标题: 查询列表数据 ByBean
     * <p>描述:  
     * @throws Exception 
     */
    public void  findListByBean() throws Exception{
    	Map<String,Object> result = new HashMap<String,Object>();
        List<Wifis> list = new ArrayList();
        try{
           HttpServletRequest request = getRequest();
           String wifisJson = request.getParameter("wifisJson");
           if (null != wifisJson&&!"".equals(wifisJson)) {
              JSONObject jsonobject =JSONObject.fromObject( wifisJson );
              Wifis wifisobj = (Wifis)JSONObject.toBean(jsonobject, Wifis.class); 
              list =  wifisManager.findListByBean(wifisobj); 
           }else{
        	  list = wifisManager.findAll(); 
           }
           result.put(SUCCESS, true);
        }catch (Exception e){ 
        	result.put(SUCCESS, false);
            e.printStackTrace();
        } 
		result.put("list", list);
        outJson(result);
    }	
}
